﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 条码信息查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BarCodeInfoController : ControllerBase
    {
        private readonly ILogger<WMSBarCodeInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_gd;


        public BarCodeInfoController(ILogger<WMSBarCodeInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_gd = _configuration.GetConnectionString("WMSDbConnection_GD");

        }

        /// <summary>
        /// 条码信息查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostBarCodeInfo([FromBody] WMSBarCodeInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSBarCodeInfoResponseDto re = new WMSBarCodeInfoResponseDto();
            string barcode_no = strparm.barcode_no;
            string jd_code = strparm.jd_code;
            string con_jd = "";
            if (jd_code=="ZH")
            {
                con_jd = con_zh;
            }
            else if(jd_code == "QH")
            {
                con_jd = con_qh;
            }
            else if (jd_code == "YB")
            {
                con_jd = con_yb;
            }
            else if (jd_code == "GD")
            {
                con_jd = con_gd;
            }
            else
            {
                strMsg = "基地编码不能为空，需要填<珠海ZH>、<青海QH>、<宜宾YB>、<广东高景GD>";
            }
            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                strSql = new StringBuilder();
                strSql.Append(@"select * from barcode_file where 1=1 and barcode_no='"+ barcode_no.Trim() + "'");
                DataTable dt_barcodecheck = DB.Query(strSql.ToString(), con_jd);
                if (dt_barcodecheck.Rows.Count<=0)
                {
                    strMsg = "WMS系统不存在《"+ barcode_no + "》条码信息，请检查清楚再调用接口";
                }
                else
                {
                    re.barcode_no = dt_barcodecheck.Rows[0]["barcode_no"].ToString();
                    re.material_code = dt_barcodecheck.Rows[0]["material_code"].ToString();
                    re.material_name = dt_barcodecheck.Rows[0]["material_name"].ToString();
                    re.material_unit = dt_barcodecheck.Rows[0]["material_unit"].ToString();
                    re.source_batch = dt_barcodecheck.Rows[0]["source_batch"].ToString();
                    re.material_amount = DB.ToDecimal(dt_barcodecheck.Rows[0]["material_amount"].ToString()) ;
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
